#!/bin/bash

basepath=$(dirname $0)
server_prefix=/zanui-weapp

# convert relative path to absolute path
function abspath() {
  pushd . > /dev/null; if [ -d "$1" ]; then cd "$1"; dirs -l +0; else cd "`dirname \"$1\"`"; cur_dir=`dirs -l +0`; if [ "$cur_dir" == "/" ]; then echo "$cur_dir`basename \"$1\"`"; else echo "$cur_dir/`basename \"$1\"`"; fi; fi; popd > /dev/null;
}

command_exists () {
    type "$1" >/dev/null 2>&1
}

fontname() {
  echo "https://b.yzcdn.cn$server_prefix/$(basename $basepath/../assets/icons/build/font/zanui-weapp-icon-*.$1)"
}

# generate font files from sketch file
$basepath/icons/extract-icons.sh
$basepath/icons/generate-font.sh

if command_exists superman ; then
  # upload to cdn
  superman cdn $server_prefix $basepath/../assets/icons/build/font/zanui-weapp-icon-*
fi

# generate fontface style
eot=$(fontname eot)
cat > $basepath/../packages/icon/index.pcss <<EOF
/* DO NOT EDIT! Generated by fount */

@font-face {
  font-family: 'zanui-weapp-icon';
  src: url('$eot');
  src: url('$eot?#iefix') format('embedded-opentype'),
       url('$(fontname woff2)') format('woff2'),
       url('$(fontname woff)') format('woff'),
       url('$(fontname ttf)') format('truetype')
}

.zan-icon {
    display: inline-block;
}
.zan-icon::before {
  font-family: "zanui-weapp-icon" !important;
  font-style: normal;
  font-weight: normal;
  speak: none;

  display: inline-block;
  text-decoration: inherit;
  width: 1em;
  text-align: center;

  /* For safety - reset parent styles, that can break glyph codes*/
  font-variant: normal;
  text-transform: none;

  /* fix buttons height, for twitter bootstrap */
  line-height: 1em;

  /* Animation center compensation - margins should be symmetric */
  /* remove if not needed */
  /* margin-left: .2em; */

  /* you can be more comfortable with increased icons size */
  /* font-size: 120%; */

  /* Font smoothing. That was taken from TWBS */
  -webkit-font-smoothing: antialiased;

  /* Uncomment for 3D effect */
  /* text-shadow: 1px 1px 1px rgba(127, 127, 127, 0.3); */
}
EOF

cat $basepath/../assets/icons/build/css/zanui-weapp-icon-codes.css >> $basepath/../packages/icon/index.pcss
